//
//  SKVistorView.swift
//  WeiboProject
//
//  Created by yanli on 16/7/9.
//  Copyright © 2016年 mountainPassCell. All rights reserved.
//

import UIKit
import SnapKit  //第三方框架需要导入

class SKVistorView: UIView {
    //登录 或 注册 执行的闭包
    var loginClosure: (()->())?
    
    // MARK: -- 懒加载
    // 旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 遮罩,罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 主页图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 消息 下面文字Lable  想加入更多东西  弄个闭包
    private lazy var messageLabel: UILabel = {
        //创建 Lable
        let lable = UILabel()
        // 设置
        lable.font = UIFont.systemFontOfSize(14)
        lable.textColor = UIColor.grayColor()
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //多行显示
        lable.numberOfLines = 0
        //设置文字居中
        lable.textAlignment = .Center
        
        return lable
    }()
    
    // 懒加载 注册 
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        //添加 注册 btn 的点击事件
        btn.addTarget(self, action: "registerButtonAction", forControlEvents: .TouchUpInside)
        //设置 文字
        btn.setTitle("注册", forState: .Normal)
        //设置 文字颜色
        btn.setTitleColor(UIColor.grayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //设置 背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        //设置字体大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return btn
    }()
    
    // 懒加载 登录
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        //添加 登录 btn 的点击事件
        btn.addTarget(self, action: "loginButtonAction", forControlEvents: .TouchUpInside)
        //设置 文字
        btn.setTitle("登录", forState: .Normal)
        //设置 文字颜色
        btn.setTitleColor(UIColor.grayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //设置 背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        //设置字体大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return btn
    }()
    
    
    // 重写 init 加载 界面
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //搭建 界面呢
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加视图  设置约束
    private func setupUI() {
        // 设置访客视图颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        // 添加到视图 在进行布局
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 使用 snapKit ,根 masonry 一样 进行布局
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)  //在视图中心
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
    }

    // MARK:  -- 点击事件
    // 注册
    func registerButtonAction() {
        print("访客视图的注册按钮点击了")
        loginClosure?()
    }
    
    //登录
    func loginButtonAction() {
        print("访客视图的登录点击了")
        loginClosure?()
    }


    //  开启动画
    private func startAnimation() {
        //创建一个 基本动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        // 目的地
        animation.toValue = 2 * M_PI
        // 动画时长
        animation.duration = 20
        // 重复次数
        animation.repeatCount = MAXFLOAT
        // 动画执行完成,不让其释放
        animation.removedOnCompletion = false
        
        // 执行动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    

    // 提供修改信息的访客视图
    func updateVisitorViewInfo(title: String?, imageName: String?) {
        if let t = title, img = imageName {
            messageLabel.text = t
            iconImageView.image = UIImage(named: img)
            // 隐藏旋转视图
            cycleImageView.hidden = true
        } else {
            //  显示旋转视图
            cycleImageView.hidden = false
            //  开启动画
            startAnimation()
        }
    }
    
}


